package com.octopus.optiworks.controller;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import com.octopus.optiworks.model.Ituch;
import com.octopus.optiworks.model.Itunm;

public class DictionaryValidator extends CommonValidator {

	@Override
    public boolean supports(Class<?> aClass) {
        return Object.class.isAssignableFrom(aClass);
    }

	@Override
    public void validate(Object o, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.name.empty", "Please specify a name.");
        
        if(!stepValided)
        	return;
        if(o instanceof Itunm)
        	this.validateItunm((Itunm)o, errors);
        else if(o instanceof Ituch)
        	this.validateItuch((Ituch)o, errors);
    }
    
    private void validateItunm(Itunm itunm, Errors errors) {
        if(itunm.getName().replace(".", "").length()!=7 || !NumberUtils.isNumber(itunm.getName()))
        	errors.rejectValue( "name", "error.product.number.invalid.itunm", "Please enter a valid number with 7 digit: ITU λ (nm)." );
    }
    
    private void validateItuch(Ituch ituch, Errors errors) {
        if(ituch.getName().replace(".", "").length()!=3 || !NumberUtils.isNumber(ituch.getName()))
        	errors.rejectValue( "name", "error.product.number.invalid.ituch", "Please enter a valid number with 3 digit: ITU CH#." );
    }
}
